﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Darwin.Source.Particle_Systems
{
    public abstract class CBaseParticleSystem
    {
        protected int m_iMaxParticles;
        protected int m_iNumParticles;
        protected int m_iEmitRate;
        protected float m_fAccumulatedTime;
        protected bool m_bIsActive;
        protected Vector3 m_v3EmitOrigin;
        protected CParticle[] m_ParticleArray;
        protected VertexPositionColor[] m_vertData;
        protected VertexDeclaration m_vdVertexPosColDecl;
        protected Game r_gGame;
        protected float m_fMinTerrainHeight;
        
        public CBaseParticleSystem(int maxParticles)
        {
            m_v3EmitOrigin = new Vector3(0, 0, 0);
            m_bIsActive = false;
            m_iMaxParticles = maxParticles;            
        }

        protected void BaseInitialise( Game game, float minHeight )
        {
            m_fMinTerrainHeight = minHeight;
            r_gGame = game;
            m_iNumParticles = 0;
            m_fAccumulatedTime = 0.0f;
            m_ParticleArray = new CParticle[m_iMaxParticles];

            for (int i = 0; i < m_ParticleArray.Length; i++)
            {
                m_ParticleArray[i] = new CParticle();
            }

            m_vertData = new VertexPositionColor[m_iMaxParticles];
        }

        public void ResetParticleOrigin( Vector3 origin )
        {
            m_v3EmitOrigin = origin;

            for (int i = 0; i < m_iMaxParticles; ++i)
            {

                m_ParticleArray[i].m_v3Position = m_v3EmitOrigin;
                m_vertData[i].Position = m_v3EmitOrigin;
            }
        }

        public void ToggleActive() { m_bIsActive = !m_bIsActive; }
        public bool IsActive() { return m_bIsActive; }

        public abstract bool Initialise(Game game, float minHeight);
        public abstract void LoadContent();
        public abstract void EmitParticles(int numParticles);
        public abstract void DoUpdate( float elapsedTime );
        public abstract void DoRender( Matrix projMatrix, Matrix viewMatrix );
        public abstract void Reaper();
        protected abstract void InitialiseParticle(int index);
    }
}
